<template>
  <div class="auto-nav">
    <el-menu router default-active="" class="el-menu-vertical" @open="handleOpen" @close="handleClose" :collapse="isCollapse" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
      <el-menu-item @click="isCollapse = !isCollapse" style="width: 100%; background: rgb(84,92,100)">
        <i :class="isCollapse ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"></i>
      </el-menu-item>
      <template v-for="(item, index) in menuList">
        <RecursiveSubmenu :key="index" :menu="item" />
      </template>
    </el-menu>
  </div>
</template>



<script>
import routes from "@/router/routes";
import RecursiveSubmenu from "./RecursiveSubmenu";

function generateNavInfo(routes, path = "") {
  if (!routes.length) {
    return [];
  }

  return routes.filter(item => {
    return !!item.meta && !!item.meta.title;
  }).map(item => {
    const index = `${path}/${item.path}`.replace(/\/\//, "/");
    console.log(index);
    const route = {
      index,
      ...item.meta,
      children: item.children ? generateNavInfo(item.children, index) : null
    };
    return route;
  });
}

export default {
  components: {
    RecursiveSubmenu
  },
  data() {
    return {
      isCollapse: true,
      menuList: []
    };
  },
  created() {
    this.menuList = generateNavInfo(routes);

    console.log(this.menuList);
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  }
};
</script>

<style lang="scss">
.auto-nav {
  height: 100%;
  .el-menu-vertical {
    &:not(.el-menu--collapse) {
      width: 200px;
      min-height: 400px;
    }
    height: 100%;
    background-color: rgb(84, 92, 100);
  }
}
</style>